package com.pan.admin.module.loanProduct.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.loanProduct.entity.LoanProductPlan;
import com.pan.admin.module.loanProduct.entity.vo.LoanProductPlanVO;
import com.pan.admin.module.loanProduct.enums.PlanTypeEnum;
import com.pan.admin.module.loanProduct.service.LoanProductPlanService;
import com.pan.admin.module.loanProduct.service.LoanProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  机构计划
 */
@RestController
@RequestMapping("/agency/plan")
public class LoanProductPlanController extends BaseController {

    @Autowired
    private LoanProductPlanService loanProductPlanService;
    @Autowired
    private LoanProductService loanProductService;

    /**
     * 查询配置机构计划列表
     */
    @PreAuthorize("@ss.hasPermi('org:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanProductPlan loanProductPlanVO) {
        startPage();
        List<LoanProductPlan> list = loanProductPlanService.listWithDataScope(PlanTypeEnum.ORG_PLAN, loanProductPlanVO);
        return getDataTable(list);
    }

//    /**
//     * 导出机构计划列表
//     */
//    @PreAuthorize("@ss.hasPermi('org:plan:export')")
//    @Log(title = "配置机构计划", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, LoanProductPlan loanProductPlanVO) {
//        List<LoanProductPlan> list = loanProductPlanService.listWithDataScope(PlanTypeEnum.ORG_PLAN, loanProductPlanVO);
//        ExcelUtil<LoanProductPlan> util = new ExcelUtil<LoanProductPlan>(LoanProductPlan.class);
//        util.exportExcel(response, list, "机构计划数据");
//    }

    /**
     * 获取配置机构计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('org:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoanProductPlan loanProductPlan = new LoanProductPlan();
        loanProductPlan.setId(id);
        return success(loanProductPlanService.getByIdWithDataScope(PlanTypeEnum.ORG_PLAN, loanProductPlan));
    }

    /**
     * 新增配置机构计划
     */
    @PreAuthorize("@ss.hasPermi('org:plan:add')")
    @Log(title = "新增配置机构计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanProductPlanVO loanProductPlanVO) {
        loanProductService.checkDataScope(loanProductPlanVO.getLoanProductId());
        return toAjax(loanProductPlanService.insertLoanProductPlan(PlanTypeEnum.ORG_PLAN, loanProductPlanVO));
    }

    /**
     * 修改配置机构计划
     */
    @PreAuthorize("@ss.hasPermi('org:plan:edit')")
    @Log(title = "修改配置机构计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanProductPlanVO loanProductPlanVO) {
        loanProductPlanService.checkDataScope(PlanTypeEnum.ORG_PLAN, loanProductPlanVO.getId());
        return toAjax(loanProductPlanService.updateLoanProductPlan(PlanTypeEnum.ORG_PLAN, loanProductPlanVO));
    }

    /**
     * 修改配置机构计划状态
     */
    @PreAuthorize("@ss.hasPermi('org:plan:edit')")
    @Log(title = "修改配置机构计划状态", businessType = BusinessType.UPDATE)
    @PutMapping("updateStatus")
    public AjaxResult editStatus(@RequestBody LoanProductPlanVO loanProductPlanVO) {
        loanProductPlanService.checkDataScope(PlanTypeEnum.ORG_PLAN, loanProductPlanVO.getId());
        return toAjax(loanProductPlanService.updateStatusById(PlanTypeEnum.ORG_PLAN, loanProductPlanVO));
    }
    /**
     * 删除配置机构计划
     */
    @PreAuthorize("@ss.hasPermi('org:plan:remove')")
    @Log(title = "删除配置机构计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(loanProductPlanService.batchDeleteById(PlanTypeEnum.ORG_PLAN, ids));
    }
}
